2 // ShareeSuggestionsDataSource.swift
5 // Created by Claudio Cambra on 2/4/24.
9 import NextcloudFileProviderKit
12 import SuggestionsTextFieldKit
14 class ShareeSuggestionsDataSource: SuggestionsDataSource {
17 var suggestions: [Suggestion] = []
18 var inputString: String = "" {
19 didSet { Task { await updateSuggestions() } }
22 init(account: Account, kit: NextcloudKit) {
23 self.account = account
27 private func updateSuggestions() async {
28 let sharees = await fetchSharees(search: inputString)
29 Logger.shareeDataSource.info("Fetched \(sharees.count, privacy: .public) sharees.")
30 suggestions = suggestionsFromSharees(sharees)
31 NotificationCenter.default.post(name: SuggestionsChangedNotificationName, object: self)
34 private func fetchSharees(search: String) async -> [NKSharee] {
35 Logger.shareeDataSource.debug("Searching sharees with: \(search, privacy: .public)")
36 return await withCheckedContinuation { continuation in
41 account: account.ncKitAccount,
42 completion: { account, sharees, data, error in
43 defer { continuation.resume(returning: sharees ?? []) }
44 guard error == .success else {
45 Logger.shareeDataSource.error(
46 "Error fetching sharees: \(error.description, privacy: .public)"
55 private func suggestionsFromSharees(_ sharees: [NKSharee]) -> [Suggestion] {
58 imageName: "person.fill",
59 displayText: $0.label.isEmpty ? $0.name : $0.label,